package com.bawei.module_mine.ui.login

import android.util.Log
import android.widget.Toast
import androidx.lifecycle.MutableLiveData
import androidx.lifecycle.viewModelScope
import com.bawei.lib_common.appContext
import com.bawei.lib_common.base.BaseViewModel
import com.bawei.lib_common.base.state.Failed
import com.bawei.lib_common.base.state.Loading
import com.bawei.lib_common.base.state.RequestEvent
import com.bawei.lib_common.base.state.Success
import com.bawei.lib_common.utils.ToastUtils
import com.bawei.lib_storage.mmkv.AppMMKV
import com.bawei.lib_storage.mmkv.AuthMMKV
import com.bawei.module_mine.model.LoginMode
import dagger.hilt.android.lifecycle.HiltViewModel
import kotlinx.coroutines.channels.Channel
import kotlinx.coroutines.flow.MutableSharedFlow
import kotlinx.coroutines.flow.MutableStateFlow
import kotlinx.coroutines.flow.StateFlow
import kotlinx.coroutines.flow.asSharedFlow
import kotlinx.coroutines.flow.receiveAsFlow
import kotlinx.coroutines.launch
import javax.inject.Inject

/**
 *作者：lxy
 *功能:
 *时间：2025/5/26
 */
@HiltViewModel
class LoginViewModel @Inject constructor(private val mRepository: LoginRepository) :
    BaseViewModel() {

    //保存当前正在显示的fragment
    private val _currentFragment = MutableStateFlow("")
    val currentFragment: StateFlow<String>
        get() = _currentFragment

    val phone: MutableStateFlow<String> = MutableStateFlow("")
    val password: MutableStateFlow<String> = MutableStateFlow("")

    private val _requestSmsCodeEvent = Channel<RequestEvent>()
    val requestSmsCodeEvent get() = _requestSmsCodeEvent.receiveAsFlow()

    //登录请求事件
    private val _requestLoginEvent = MutableSharedFlow<RequestEvent>()
    val requestLoginEvent get() = _requestLoginEvent.asSharedFlow()

    //校验验证码事件
    private val _checkSmsCodeEvent = MutableSharedFlow<RequestEvent>()
    val mCheckSmsCodeEvent get() = _checkSmsCodeEvent.asSharedFlow()

    var requestRest: Boolean = false

    //请求发送验证码的时间
    var sendSmsCodeTimeStamp: Long = 0L
    fun setShowingFragmentTag(tag: String) {
        _currentFragment.value = tag
    }

    fun resetSmsCodeEvent(success: Boolean) {
        viewModelScope.launch {
            _requestSmsCodeEvent.send(if (success) Success else Failed(""))
        }
    }


    fun sendSmsCode(type: String) {
        execute(
            action =
            if (type == "login") {
                mRepository.sendLoginCode("2", phone.value)
            } else {
                mRepository.sendForgetPwdCode("2", phone.value)
            },
            onStart = {
                _requestSmsCodeEvent.send(Loading)
            },
            onError = {
                _requestSmsCodeEvent.send(Failed(it))
            },
            onFinish = {
                _requestSmsCodeEvent.send(Success)
                ToastUtils.showLong(appContext,"发送成功")
                Log.d("TAG", "getSmsCode: " + it.msg)
            })
    }


    //验证码登录
    fun loginBySmsCode(smsCode: String) {
        execute(
            action = mRepository.login(LoginMode.SMS,phone.value,"",smsCode),
            onStart = {
                _requestLoginEvent.emit(Loading)
            },
            onError = {
                _requestLoginEvent.emit(Failed(it))
            },
            onFinish = {
                _requestLoginEvent.emit(Success)
                ToastUtils.showLong(appContext,"登录成功")
                AuthMMKV.access_token = it.data.access_token
            }
        )
    }

    fun clearPhone() {
        phone.value = ""
    }


    fun clearPassword() {
        password.value = ""
    }
}